 
import Vue from 'vue'
import api from "./../../api/GetUser"
import user from "./../../utils/user"
import * as types from '../mutation-types'
import VueLocalStorage  from 'vue-localstorage'
import Cookies from 'js-cookie'
import { Message } from 'iview'

// initial state
const state = {
    token:'',
}

// getters
const getters = {
  'token':state => {
    return state.token
  },
}



// actions
const actions = {

  loginForm ({ commit }, opt) { 
    api.getloginForm(opt.form).then(reply => {
       let data = reply.data;
       if(data.ok){
          commit(types.GET_USER_LOGIN, data.data); 
          opt.callback && opt.callback();
       }else{ 
        let msg = data.msg;
        msg = msg instanceof Array ?  msg[0] : msg;
        Message.error(msg); 
       }
    })
  },  

};



// mutations
const mutations = {

  [types.GET_USER_LOGIN] (state, data) {
    localStorage.setItem('token',data.token);
    if(data.expires){ //存在过期时间
      Cookies.set('lt_token',data.token, { expires: new Date(data.expires) });
    }else{  //否过期时间设为一天
      Cookies.set('lt_token',data.token, { expires: 1 });
    }
    data.access ? localStorage.setItem('access',JSON.stringify(data.access)) : '';
    state.token = data.token;
    localStorage.setItem('uname',data.uname);
  }, 

};

export default {
  state,
  getters,
  actions,
  mutations
}

